<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表白墙</title>

    <!-- 存放CSS样式 -->
    <style>
        .container {
            width: 350px;
            height: 300px;
            margin: 0 auto;
            /* border: 1px black solid; */
            text-align: center;
        }

        .grey {
            color: grey;
        }

        .container .row {
            width: 350px;
            height: 40px;

            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .container .row input {
            width: 260px;
            height: 30px;
        }

        #submit {
            width: 350px;
            height: 40px;
            background-color: orange;
            color: white;
            border: none;
            margin: 10px;
            border-radius: 5px;
            font-size: 20px;
        }
    </style>

</head>

<body>
    <div class="container">
        <h1>表白墙</h1>
        <p class="grey">输入后点击提交, 会将信息显示下方空白处</p>
        <div class="row">
            <span>谁:</span> <input type="text" name="" id="from">
        </div>
        <div class="row">
            <span>对谁:</span> <input type="text" name="" id="to">
        </div>
        <div class="row">
            <span>说什么:</span> <input type="text" name="" id="message">
        </div>
        <input type="button" value="提交" id="submit" onclick="submit()">
        <!-- <div>A 对 B 说: hello</div> -->
    </div>

    <!-- 引入JQuery -->
    <script src="./jquery-3.7.1.min.js"></script>

    <script>
        // 界面一加载就需要对后端发起Ajax请求，拿到所有的留言信息
        load();

        function load() {
            $.ajax({
                "type": "get",
                "url": "/message/getList",
                "success": function (messages) {
                    if (messages != null && messages.length > 0) {
                        let resultHtml = '';
                        // 遍历列表，添加信息
                        for (const message of messages) {
                            resultHtml += "<div>"+message.from+"对"+message.to+"说: "+message.message+"</div>";
                        }
                        // 将最终的结果添加到HTML标签上
                        $(".container").append(resultHtml);
                    }
                }
            });
        }

        // 点击提交按钮时，向后端发起Ajax请求，插入留言信息
        function submit() {
            // 获取留言信息，构造需要传输的对象
            let from = $("#from").val();
            let to = $("#to").val();
            let message =$("#message").val();
            if (from == null || to == null || message == null) {
                alert("不能输入空信息")
            }
            let data = {
                "from": from,
                "to": to,
                "message": message
            };

            $.ajax({
                "type": "post",
                "url": "/message/publish",
                // 修改请求的Content-Type为application/json
                "contentType": "application/json",
                "data": JSON.stringify(data), // 得序列化为JSON数据
                "success": function(result) {
                    // result 也是JSON数据，得转换为JS中的对象
                    let obj = JSON.parse(result);
                    // 如果插入成功，就把留言添加到页面
                    if (obj.OK == 1) {
                        let html = "<div>"+from+"对"+to+"说: "+message+"</div>";
                        $(".container").append(html);
                    } else {
                        alert("留言发布失败");
                    }
                    // 清空输入框
                    $("#from").val("");
                    $("#to").val("");
                    $("#message").val("");
                }
            });
        };
    </script>

</body>

</html>